{{define "/user/register.html"}}
<!DOCTYPE html>
<html>

<head>
    <title>IM-注册</title>
    {{template "/layout/head.html"}}
</head>

<body>

    <header class="mui-bar mui-bar-nav">
        <h1 class="mui-title">注册</h1>
    </header>
    <div class="mui-content" id="app">
        <form id='login-form' class="mui-input-group">
            <div class="mui-input-row">
                <label>账号</label>
                <input required v-model="user.username" placeholder="手机号或邮箱" type="text"
                    class="mui-input-clear mui-input">
            </div>
            <div class="mui-input-row">
                <label>昵称</label>
                <input required v-model="user.nickname" placeholder="请输入昵称" type="text"
                    class="mui-input-clear mui-input">
            </div>
            <div class="mui-input-row">
                <label>密码</label>
                <input required v-model="user.password" placeholder="请输入密码" type="password"
                    class="mui-input-clear mui-input">
            </div>
        </form>
        <div class="mui-content-padded">
            <button @click="register" type="button" class="mui-btn mui-btn-block mui-btn-primary">注册</button>
            <div class="link-area">
                <a>已有账号？</a>
                <span class="spliter">|</span>
                <a id='reg' href="login">登录</a>
            </div>
        </div>
        <div class="mui-content-padded oauth-area">
        </div>
    </div>
</body>

</html>
<script>
    var app = Vue.createApp({
        data: function () {
            return {
                user: {
                    mobile: "",
                    password: "",
                    nickname: '',
                }
            }
        },
        methods: {
            register: function () {
                if (this.user.nickname === '' || this.user.password === '' || this.user.username === '')
                    return mui.alert("字段不能为空")

                $.ajax({
                    url: "register",
                    type: "post",
                    data: {
                        username: this.user.username,
                        password: this.user.password,
                        nickname: this.user.nickname
                    },
                    success: function (res) {
                        if (res.code !== 0) {
                            return mui.toast(res.msg)
                        } else {
                            mui.toast("注册成功,正在跳转登录页面");
                            setTimeout(" window.location.href = 'login';", 500);
                        }
                    },
                    error: function (res) {
                        mui.toast("系统异常，请稍后再试");
                    }
                })
            },
        }
    })
    app.mount("#app");
</script>
{{end}}